<?php

session_start();
include ('../Fonctions.php');
$currentDate = date("d/m/Y");


$i=1;
while (isset($_POST['document'.$i.'_Obtenu'])) {
    ${"obtention" . $i} = $_POST['document'.$i.'_Obtenu'];    
    ${"date" . $i} = ajustDate(${"obtention" . $i}, $currentDate);      //ajustement de la date. la fonction ajustDate met le résultat dans $date1 ou $date2 etc
    $recuperationDate->execute(array(
        'id_etude' => $_SESSION['id_etude'],
        'id_organisme' => $_POST['organisme'],
        'id_document' => $_POST['document'.$i]
    ));
    $recuperationDateDocumentEnCours = $recuperationDate->fetch();
    if (${"obtention" . $i} == 1) {
        if ($recuperationDateDocumentEnCours['date_obtention'] != "00/00/0000") {
            ${"date" . $i} = $recuperationDateDocumentEnCours['date_obtention'];            
        }
    }
    //echo ${"obtention".$i} . "<br/"; 
    echo ${"date".$i} . "<br />";
    $i++;
}

//POUR UPDATE  ==>  boucle for
//
$i=1;
while (isset($_POST['document'.$i.'_Obtenu'])) {
    $actualisationDate->execute(array(
        'obtenu' => ${"obtention" . $i},
        'date_obtention' => ${"date" . $i},
        'id_etude' => $_SESSION['id_etude'],
        'id_organisme' => $_POST['organisme'],
        'id_document' => $_POST['document'.$i]
    ));
        $i++;
}

function ajustDate($doc, $currentDate) {
    if ($doc == 0) {
        $date = "00/00/0000";
    } else if ($doc == 1) {
        $date = $currentDate;
    }
    return $date;
}

?>
